home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / smalltlk.zip / SOURCES / OBJECT.C < prev    next >
Text File  |  1990-09-12  |  7KB  |  257 lines

  1. /*
  2.         Little Smalltalk
  3.  
  4.             object memory management
  5.  
  6.         timothy a. budd, 10/84
  7. */
  8. /*
  9.     The source code for the Little Smalltalk System may be freely
  10.     copied provided that the source of all files is acknowledged
  11.     and that this condition is copied with each file.
  12.  
  13.     The Little Smalltalk System is distributed without responsibility
  14.     for the performance of the program and without any guarantee of
  15.     maintenance.
  16.  
  17.     All questions concerning Little Smalltalk should be addressed to:
  18.  
  19.         Professor Tim Budd
  20.         Department of Computer Science
  21.         Oregon State University
  22.         Corvallis, Oregon
  23.         97331
  24.         USA
  25. */
  26. # include <stdio.h>
  27. # include "object.h"
  28. # include "drive.h"
  29. # include "string.h"
  30. # include "symbol.h"
  31. # include "byte.h"
  32. # include "number.h"
  33. # include "interp.h"
  34. # include "process.h"
  35. # include "block.h"
  36. # include "file.h"
  37. # include "primitiv.h"
  38.  
  39. # define DEBUG 0
  40.  
  41. extern object *o_acollection;
  42.  
  43. int n_incs = 0;        /* number of increments counter */
  44. int n_decs = 0;        /* number of decrements counter (should be equal)*/
  45. int n_mallocs = 0;    /* number of mallocs counter */
  46.  
  47. /* o_alloc - allocate a block of memory, checking for end of memory */
  48. char *o_alloc(n)
  49. unsigned n;
  50. {  char *p, *malloc();
  51.  
  52.    p = malloc(n);
  53.    if (p == (char *) 0) cant_happen(1);    /* out of memory */
  54.    n_mallocs++;
  55.    return(p);
  56. }
  57.  
  58. #ifndef INLINE
  59.  
  60. /* obj_inc - increment an object (usually expanded in-line) */
  61. obj_inc(x)
  62. register object *x;
  63. {
  64.     x->ref_count++;
  65.     n_incs++;
  66. }
  67.  
  68. /* obj_dec - decrement an object (usually half expanded in-line) */
  69. obj_dec(x)
  70. object *x;
  71. {
  72.     n_decs++;
  73.     if (--(x->ref_count) > 0) return;
  74. # endif
  75. # ifdef INLINE
  76. ob_dec(x)
  77. object *x;
  78. {
  79. # endif
  80.     if (x->ref_count < 0) {
  81.         fprintf(stderr,"ref count %d %d\n", x->ref_count, x);
  82.         primitive(REFCOUNTERROR, 1, &x);
  83.         return;
  84.         }
  85.     if (is_bltin(x)) {    /* free a built-in object */
  86.         switch(x->size) {
  87.             case BLOCKSIZE:
  88.                 free_block(x); break;
  89.             case BYTEARRAYSIZE:
  90.                 free_bytearray((bytearray *) x); break;
  91.             case CLASSSIZE :
  92.                 free_class((class *) x); break;
  93.             case FILESIZE:
  94.                 free_file((struct file_struct *) x);
  95.                 break;
  96.             case FLOATSIZE:
  97.                 free_float((sfloat *) x); break;
  98.             case INTEGERSIZE: case CHARSIZE:
  99.                 free_integer((integer *) x); break;
  100.             case INTERPSIZE:
  101.                 free_terpreter((interpreter *) x); break;
  102.             case PROCSIZE:
  103.                 free_process((process *) x); break;
  104.             case SYMBOLSIZE:
  105.                 cant_happen(16);
  106.             case STRINGSIZE:
  107.                 free_string((string *) x); break;
  108.             default: cant_happen(6);
  109.             }
  110.         }
  111.     else {        /* free a normal (non-special) object */
  112.         if (x->super_obj)
  113.             obj_dec(x->super_obj);
  114.         free_obj(x, 1);
  115.         }
  116. }
  117.  
  118. # define MAXOBJLIST 100
  119. # define sizeobj(x) (sizeof(object) + ((x) - 1) * sizeof(object *) )
  120.  
  121. /* obj_free_list is a free list for memory blocks */
  122.  
  123. static object *obj_free_list[MAXOBJLIST]; /* better be initialized to zero! */
  124.  
  125. int ca_obj = 0;            /* count the number of allocations made */
  126. int ca_cobj[5] = {0,0,0,0,0};    /* count how many allocations for small vals*/
  127.  
  128. /* make sure the following list is null terminated! */
  129. int size_obj_init[] = {15, 75, 420, 10, 10, 5, 0};
  130.  
  131. /* init_objs - initialize the memory management module */
  132. init_objs() {
  133.     int i, j, max, size;
  134.     char *p;
  135.     object *new;
  136.  
  137.     for (i = 0; (max = size_obj_init[i]); i++) {
  138.         size = sizeobj(i);
  139.         p = o_alloc((unsigned int) (max * size));
  140.         for (j = 0; j < max; j++) {
  141.             new = (object *) p;
  142.             new->super_obj = obj_free_list[i];
  143.             obj_free_list[i] = new;
  144.             p += size;
  145.             }
  146.         }
  147. }
  148.  
  149. /* new_obj - create a new non-special object */
  150. object *new_obj(nclass, nsize, alloc)
  151. class *nclass;
  152. int nsize, alloc;
  153. {    register object *new;
  154.     int i;
  155.     
  156.     if (nsize < 0)
  157.         cant_happen(2);
  158.     if (nsize < MAXOBJLIST && obj_free_list[nsize])
  159.         obj_free_list[nsize] = (new = obj_free_list[nsize])->super_obj;
  160.     else {
  161.         new = (object *) o_alloc(sizeobj(nsize));
  162.         ca_obj++;
  163.         if (nsize < 5)
  164.             ca_cobj[nsize]++;
  165.     }
  166.     new->super_obj = (object *) 0;
  167.     new->class = nclass;
  168.     if (nclass)
  169.         obj_inc((object *) new->class );
  170.     new->ref_count = 0;
  171.     new->size = nsize;
  172.     if (alloc)
  173.         for (i = 0; i < nsize; i++) {
  174.             sassign(new->inst_var[ i ], o_nil);
  175.          }    
  176.     return(new);
  177. }
  178.     
  179. /* free_obj - free a non-special object */
  180. free_obj(obj, dofree)
  181. register object *obj;
  182. int    dofree;
  183. {    int size, i;
  184.  
  185.     size = obj->size;
  186.     if (dofree)
  187.         for (i = 0; i < size; i++)
  188.             obj_dec(obj->inst_var[i]);
  189.     if (obj->class)
  190.         obj_dec((object *) obj->class);
  191.     if (size < MAXOBJLIST) {
  192.         obj->super_obj = obj_free_list[size];
  193.         obj_free_list[size] = obj;
  194.         }
  195.     else {
  196.         free(obj);
  197.               }
  198. }
  199.  
  200. /* fnd_class - find the class of a special object */
  201. object *fnd_class(anObject)
  202. object *anObject;
  203. {    object *result, *lookup_class();
  204.     char *name;
  205.  
  206.     if (is_bltin(anObject)) {
  207.         switch(anObject->size) {
  208.             case BLOCKSIZE:   name = "Block"; break;
  209.             case CLASSSIZE:   name = "Class"; break;
  210.             case FILESIZE:    name = "File"; break;
  211.             case FLOATSIZE:   name = "Float"; break;
  212.             case INTEGERSIZE: name = "Integer"; break;
  213.             case CHARSIZE:    name = "Char"; break;
  214.             case INTERPSIZE:  name = "Interp"; break;
  215.             case PROCSIZE:    name = "Process"; break;
  216.             case SYMBOLSIZE:  name = "Symbol"; break;
  217.             case STRINGSIZE:  name = "String"; break;
  218.             case BYTEARRAYSIZE: name = "ByteArray"; break;
  219.             default: cant_happen(6);
  220.             }
  221.         result = lookup_class(name);
  222.         }
  223.     else
  224.         result = (object *) anObject->class;
  225.     return(result);
  226. }
  227.  
  228. extern object *o_object, *o_magnitude, *o_number;
  229.  
  230. /* fnd_super - produce a super-object for a special object */
  231. object *fnd_super(anObject)
  232. object *anObject;
  233. {    object *result;
  234.  
  235.     if (is_bltin(anObject)) {
  236.         switch(anObject->size) {
  237.             case BLOCKSIZE:   result = o_object; break;
  238.             case CLASSSIZE:   result = o_object; break;
  239.             case FILESIZE:    result = o_object; break;
  240.             case FLOATSIZE:   result = o_number; break;
  241.             case INTEGERSIZE: result = o_number; break;
  242.             case CHARSIZE:    result = o_magnitude; break;
  243.             case INTERPSIZE:  result = o_object; break;
  244.             case PROCSIZE:    result = o_object; break;
  245.             case SYMBOLSIZE:  result = o_object; break;
  246.             case STRINGSIZE:   /* strings DO have superobjs*/
  247.                 result = ((string *) anObject)->s_super_obj;
  248.                 break;
  249.             case BYTEARRAYSIZE: result = o_acollection; break;
  250.             default: cant_happen(6);
  251.             }
  252.         }
  253.     else
  254.         result = anObject->super_obj;
  255.     return(result);
  256. }
  257.